package net.wantongtech.xa.uploadbasicinfo.run;

import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.service.*;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 命令行指令运行器
 *
 * @author MaShibo
 * 2021/2/20 0020
 */
@Slf4j
@Component
public class CustomCommandLineRunner implements CommandLineRunner {

    @Resource
    private ReFillCardBlackListRecordService reFillCardBlackListRecordService;

    @Resource
    private MigratePortalTransOutDataService migratePortalTransOutDataService;

    @Resource
    private DownloadExpandPackageService downloadExpandPackageService;

    @Resource
    private LoadReceiveExpandMsgService loadReceiveExpandMsgService;

    @Resource
    private GeneratePortalTransOutViewScriptService generatePortalTransOutViewScriptService;

    @Resource
    private DownloadReceiveFileService downloadReceiveFileService;


    @Override
    public void run(String... args) throws Exception {
        if (args == null || args.length == 0) {
            log.info("请输入要运行的服务");
            return;
        }

        String type = args[0];
        if ("1".equals(type)) {

            if (args.length < 2) {
                log.warn("请输入卡片黑名单文件的保存路径");
                return;
            }
            log.info("重新填充卡片黑名单数据...");
            String savePath = StringUtils.trimToNull(args[1]);
            reFillCardBlackListRecordService.setSavePath(savePath);
            try {
                reFillCardBlackListRecordService.fill();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                List<String> validateErrorMsgList = reFillCardBlackListRecordService.getValidateErrorMsgList();
                if (!validateErrorMsgList.isEmpty()) {
                    String validateErrorMsg = String.join("\n", validateErrorMsgList);
                    log.info("校验错误信息：\n{}", validateErrorMsg);
                }
                validateErrorMsgList.clear();
            }
        } else if ("2".equals(type)) {
            if (args.length < 2) {
                log.warn("请输入最大同步日期");
                return;
            }
            migratePortalTransOutDataService.setMaxDate(LocalDate.parse(args[1]));
            migratePortalTransOutDataService.migrate();
        } else if ("3".equals(type)) {
            if (args.length < 2) {
                log.warn("请输入起始版本号");
                return;
            }
            try {
                downloadExpandPackageService.download(args[1]);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        } else if ("4".equals(type)) {
            try {
                loadReceiveExpandMsgService.load();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        } else if ("5".equals(type)) {
            if (args.length < 2) {
                log.warn("请输入脚本文件路径");
                return;
            }
            try {
                generatePortalTransOutViewScriptService.generate(args[1]);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        } else if ("6".equals(type)) {
            if (args.length < 3) {
                log.warn("请输入目标路径、下载数量和对应bizcode");
                return;
            }
            int[] bizCodes = Arrays.stream(ArrayUtils.subarray(args, 3, args.length)).mapToInt(Integer::parseInt).toArray();
            downloadReceiveFileService.downloadByBizCode(args[1], Integer.parseInt(args[2]), bizCodes);
        }

    }
}
